<?php

/* * *
 * 微信粉丝关注列表
 */

namespace Api\Controller;

use Think\Controller;

class FansController extends Controller {

    public function index() {
        echo "api v1.0";
    }
    /**
     * 根据openid更新
     * @param type $openid
     * @return boolean
     * 
     */
    function updateFansByOpenid($openid) {

        if ($openid) {
            $fsdata = M('lvdi_fans.openid')->where(array('openid' => $openid))->find();
            $this->doAdd($openid);
            if ($fsdata) {
                return true;
            } else {
                return false;
            }
        } else {
            return;
        }
    }
    /**
     * 拉取微信用户列表
     * @param type $next_openid
     * @return type
     * 
     */
    private function cgibin_wxopenid($next_openid = null) {
        $url = "https://api.weixin.qq.com/cgi-bin/user/get?access_token=" . wx_access_token() . "&next_openid=" . $next_openid;
        $data = http_get($url);
        $openlist = json_decode($data, true);
        //$openidArr = $openlist['data']['openid'];
        return $openlist;
    }
    /**
     * 处理用户列表并保存
     * @param type $openid
     * 
     */
    private function doAdd($openid) {
        set_time_limit(0);
        $data = S('openid_caches' . $openid);
        if (empty($data)) {
            //echo 111;exit;
            $dbdata = M('lvdi_fans.openid')->select();
            foreach ($dbdata as $key => $value) {
                $arr[] = $value['openid'];
            }

            //10000
            $openlist1 = $this->cgibin_wxopenid();
            //echo $openlist['total'];exit;
            $openidArr1 = $openlist1['data']['openid'];
            
            //大于10000
            if ($openlist1['total'] > 10000) {
                //echo array_slice($openidArr,-1,1)[0];exit;
                $openlist2 = $this->cgibin_wxopenid(array_slice($openidArr1, -1, 1)[0]);//最后一条用户openid
                $openidArr2 = $openlist2['data']['openid'];
            }
            $openidArr = array_merge($openidArr1, $openidArr2);

            //print_r($openidArr);exit;
           // $c = array_intersect($openidArr, $arr);//数组的交集
            $b = array_diff($openidArr, $arr);//返回差集
            //$d = array_merge(array_diff($openidArr, $arr));
            // print_r($d);  echo count($openidArr);exit;

            $datalist = array();
//            echo '<br>';
//            echo count($b);
//            print_r($b);
//            echo '<br>';
//            echo count($c);
//            //   print_r($c);
//            echo '<br>';
//            // print_r($openidArr);
//            echo count($openidArr);
            foreach ($b as $key => $value) {
                $datalist[] = array('openid' => $b[$key]);
            }
            //print_r($datalist);
            M('lvdi_fans.openid')->addAll($datalist);
            if (count($b) > 0) {
                S('openid_caches' . $openid, $openid, 3600 * 4);
            }
            //exit;
        }
    }

    public function checkFans() {
        $openid = I('openid');
        if (!$openid)
            $this->ajaxReturn(array('code' => 0, 'msg' => 'no openid'));;
        //是否关注
        $op = M('lvdi_fans.openid')->where(array('openid' => $openid))->find();
        if ($op)
            $this->ajaxReturn(array('code' => 1, 'msg' => 'exit fans'));
        else {
            $userlist = $this->userlist();
            //print_r($userlist);
            if (in_array($openid, $userlist)) {
                M('lvdi_fans.openid')->add(array('openid' => $openid));
                $this->ajaxReturn(array('code' => 1, 'msg' => 'just fans'));
            } else {//非关注者                
                $this->ajaxReturn(array('code' => 0, 'msg' => 'not fans'));
            }
        }
    }

    public function userlist() {
        $url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=' . wx_access_token();
        $data = $this->http_get($url);
        $userlist = json_decode($data, true);
        if (I('debug'))
            print_r($userlist);
        else
            return $userlist['data']['openid'];
    }

    public function http_get($url) {
        $oCurl = curl_init();
        if (stripos($url, "https://") !== FALSE) {
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYPEER, FALSE);
            curl_setopt($oCurl, CURLOPT_SSL_VERIFYHOST, FALSE);
            curl_setopt($oCurl, CURLOPT_SSLVERSION, 1); //CURL_SSLVERSION_TLSv1
        }
        curl_setopt($oCurl, CURLOPT_URL, $url);
        curl_setopt($oCurl, CURLOPT_RETURNTRANSFER, 1);
        $sContent = curl_exec($oCurl);
        $aStatus = curl_getinfo($oCurl);
        curl_close($oCurl);
        if (intval($aStatus["http_code"]) == 200) {
            return $sContent;
        } else {
            return false;
        }
    }

    public function _empty() {
        
    }

}
